'''
Created on May , 2012

@author: lavang
'''
import unittest
from Interface.professorPage import xpathTests
from Interface.testsPage import xpathLinkAddTest, xpathCbbParentUnit,\
    xpathTxtName, xpathTxtDuration, xpathTxtRepetition, xpathTxtMaintainHistory,\
    xpathTxtMasterScore, xpathBtnSubmitTest, xpathLinkTests
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common import keys
from selenium.webdriver.support.wait import WebDriverWait

# click on Tests img
def clickOnTests(self):
    browser=self.browser
    element=browser.find_element_by_xpath(xpathTests())
    element.click()
    
# click Add test
def clickOnAddTest(self):
    browser=self.browser
    element=browser.find_element_by_xpath(xpathLinkAddTest())
    element.click()
    
# add Test
def addTest(self,name,description,parentUnit='---Root unit----',duration=10,repetition=1,maintainHistory='',masteryScore=50,edit='off'):
    browser=self.browser
    if edit=='on':
        pass
    else:
        element=browser.find_element_by_xpath(xpathCbbParentUnit())
        Select(element).select_by_visible_text(parentUnit)
    
    # enter test name
    element=browser.find_element_by_xpath(xpathTxtName())
    element.clear()
    element.send_keys(name)
    
    # duration
    element=browser.find_element_by_xpath(xpathTxtDuration())
    element.clear()
    element.send_keys(duration)
    
    # repetition
    element=browser.find_element_by_xpath(xpathTxtRepetition())
    element.clear()
    element.send_keys(repetition)
    
    # maintain history
    element=browser.find_element_by_xpath(xpathTxtMaintainHistory())
    element.clear()
    element.send_keys(maintainHistory)
    
    # mastery score
    element=browser.find_element_by_xpath(xpathTxtMasterScore())
    element.clear()
    element.send_keys(masteryScore)
    
    # description
    browser.switch_to_frame("editor_content_data_ifr")
    element=browser.find_element_by_xpath(".//*[@id='tinymce']")
    element.send_keys(Keys.CONTROL,'a')
    element.send_keys(Keys.DELETE)
    element.send_keys(description)
    
    browser.switch_to_default_content()
    # Submit
    element=browser.find_element_by_xpath(xpathBtnSubmitTest())
    element.click()
    
# back to Tests page
def clickOnLinkTest(self):
    browser=self.browser
    element=browser.find_element_by_xpath(xpathLinkTests())
    element.click()
    
# verify test is added
def isTestAddedSuccessfully(self,test,parentUnit):
    browser = self.browser
    try:
        WebDriverWait(browser,20).until(lambda browser : browser.find_element_by_xpath("//a[text()='"+test+"']/../../td[text()='"+parentUnit+"']"))
        return True
    except:
        return False
    
# click on a Test
def clickOnATest(self,test):
    browser=self.browser
    element=browser.find_element_by_xpath("//a[text()='"+test+"']")
    element.click()
    
# delete Test
def deleteTest(self,test,parentUnit):
    browser=self.browser
    element=browser.find_element_by_xpath("//a[text()='"+test+"']/../../td[text()='"+parentUnit+"']/../td[6]/img")
    element.click()
    alert=browser.switch_to_alert()
    alert.accept()
    browser.refresh()
    
# verify that a test is deleted
def isTestDeletedSuccessfully(self,test,parentUnit):
    browser = self.browser
    try:
        WebDriverWait(browser,20).until(lambda browser : browser.find_element_by_xpath("//a[text()='"+test+"']/../../td[text()='"+parentUnit+"']/../td[6]/img"))
        return True
    except:
        return False